package com.ssm.util.alg.redpocket;

import java.util.ArrayList;
import java.util.Random;


public class RodomLeftMoney {
	
	/** 
     *  
     * @param total 
     *            总金额 
     * @param splitCount 
     *            个数 
     * @param min 
     *            最小金额 
     * @param max 
     *            最大金额 
     */  
    public static ArrayList<Integer> splitRedPacket(int total,int splitCount, int min, int max) {   
        ArrayList<Integer> al = new ArrayList<Integer>();  
        Random random = new Random();  
  
        if ((splitCount & 1) == 1) {// 奇数个红包，需要单独将其中一个红包先生成，以保证后续算法拆分份数为偶数。  
            System.out.println("红包个数{"+splitCount+"}是奇数，单独生成一个红包");  
            int num = 0;  
            do {  
                num = random.nextInt(max);  
                // num = (total - num) % (splitCount / 2) + num; //  
                // 将后面算法拆分时的余数加入到这个随机值中。  
                System.out.println("单个的随机红包为：{"+num+"}");  
            } while (num >= max || num <= min);  
  
            total = total - num;  
            al.add(num);  
        }  
        int couples = splitCount >> 1;  
        int perCoupleSum = total / couples;  
  
        if ((splitCount & 1) == 1) {  
             System.out.println("处理后剩余的金额为：{"+total+"}");  
        }  
  
        for (int i = 0; i < couples; i++) {  
            Boolean finish = true;  
            int num1 = 0;  
            int num2 = 0;  
            do {  
                num1 = random.nextInt(max);  
                num2 = perCoupleSum - num1;  
                if (!al.contains(num1) && !al.contains(num2)) {  
                    if (i == 0) {  
                        num1 = (total - couples * perCoupleSum) + num1;  
                    }  
                }  
            } while (num1 < min || num1 > max || num2 < min || num2 > max);  
            al.add(num1);  
            al.add(num2);  
        }  
  
        Integer.compare(1, 2);  
//        al.sort(new Comparator<Integer>() {  
//            @Override  
//            public int compare(Integer o1, Integer o2) {  
//                return Integer.compare(o1, o2);  
//            }  
//        });  
        return al;
    }
    
    public static double getRandomMoney(LeftMoneyPackage _leftMoneyPackage) {
        // remainSize 剩余的红包数量
        // remainMoney 剩余的钱
    	System.out.println("剩余红包个数："+_leftMoneyPackage.remainSize+"剩余钱："+_leftMoneyPackage.remainMoney);
        if (_leftMoneyPackage.remainSize == 1) {
            _leftMoneyPackage.remainSize--;
            return (double) Math.round(_leftMoneyPackage.remainMoney * 100) / 100;
        }
        double pingjun=20000/22;
        double qishi=(pingjun-(1000-pingjun));
        double money = Math.random() * (1000-qishi)+qishi;
//        while(money<100){
//        	money = Math.random() * (1000-qishi)+qishi;
//        }
        money = Math.floor(money * 100) / 100;
        _leftMoneyPackage.remainSize--;
        _leftMoneyPackage.remainMoney -= money;
        
        return money;
    }
    
    public static void main(String[] args) {
//    	System.out.println("开始");
//    	ArrayList<Integer> splitRedPacket = splitRedPacket(10000, 20, 100, 1000);
//    	System.out.println(splitRedPacket);
//    	System.out.println("结束");
    	ArrayList<Double> al = new ArrayList<Double>();
    	LeftMoneyPackage leftMoneyPackage = new LeftMoneyPackage();
    	leftMoneyPackage.setRemainMoney(20000.00);
    	leftMoneyPackage.setRemainSize(22);
    	for (int i = 0; i < 22; i++) {
    		if (i%2==1) {
//    			System.out.println("剩余红包个数："+leftMoneyPackage.remainSize+"剩余钱："+leftMoneyPackage.remainMoney);
    			al.add((20000/22)*2-al.get(i-1));
			}else {
				double money = getRandomMoney(leftMoneyPackage);
				al.add(money);
			}
		}
    	System.out.println(al);
    	double sum=0;
    	for (Double double1 : al) {
    		sum+=double1;
		}
    	System.out.println(sum);
    	
	}
}
